package com.ecoolex.discount.sales.core.manager;

import java.sql.Date;
import java.time.Instant;

import com.ecoolex.discount.sales.entity.YWGiftCardOrderExample;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecoolex.discount.sales.core.dao.YWGiftCardOrderDAO;
import com.ecoolex.discount.sales.entity.YWBrand;
import com.ecoolex.discount.sales.entity.YWGiftCardOrder;
import com.ecoolex.discount.sales.pojo.GenerateCtripOrderDTO;

@Service
public class YWGiftCardOrderManager {
	@Autowired
	private YWGiftCardOrderDAO dao;

	public YWGiftCardOrder save(GenerateCtripOrderDTO generateCtripOrderDTO) {
		YWGiftCardOrder ywGiftCardOrder = new YWGiftCardOrder();
		BeanUtils.copyProperties(generateCtripOrderDTO, ywGiftCardOrder);
		ywGiftCardOrder.setStatus(1);
		ywGiftCardOrder.setDeleted(YWBrand.NOT_DELETED);
		ywGiftCardOrder.setCreateTime(Date.from(Instant.now()));
		ywGiftCardOrder.setCreateUser("system-admin");
		ywGiftCardOrder.setUpdateUser("system-admin");
		ywGiftCardOrder.setUpdateTime(Date.from(Instant.now()));
		dao.insert(ywGiftCardOrder);
		return ywGiftCardOrder;
	}

	public YWGiftCardOrder updateStatus(YWGiftCardOrder ywGiftCardOrder) {
		ywGiftCardOrder.setUpdateUser("system-admin");
		ywGiftCardOrder.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(ywGiftCardOrder, YWGiftCardOrder.Column.status,
				YWGiftCardOrder.Column.updateUser, YWGiftCardOrder.Column.updateTime);
		return ywGiftCardOrder;
	}
	public YWGiftCardOrder getYwGiftCardOrder(String serviceId,String channelId,String orderNo) {
		YWGiftCardOrderExample.Criteria criteria = YWGiftCardOrderExample.newAndCreateCriteria() //
				.andServiceIdEqualTo(serviceId) //
				.andChannelIdEqualTo(channelId) //
				.andOrderNoEqualTo(orderNo);
		return dao.selectOneByExample(criteria.example());
	}

}
